package com.imooc.springboot.dubbo.nilinili.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.nilinili.entity.dto.CommentDTO;
import com.imooc.springboot.dubbo.nilinili.res.CodeEnum;
import com.imooc.springboot.dubbo.nilinili.res.ResultFactory;
import com.imooc.springboot.dubbo.nilinili.service.CommentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/comment")
@Validated
public class CommentController {
    @Reference
    private CommentService commentService;

    /**
     *  根据图片id获取评论
     * @param id
     * @return
     */
    @GetMapping("/selectCommentByPhotoId/{id}")
    public String selectCommentByPhotoId(@PathVariable("id") Integer id) {
        List<CommentDTO> commentDTOS = commentService.selectCommentByPhotoId(id);
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询评论成功",commentDTOS);
    }

    /**
     *  添加新的评论
     * @param data
     * @return
     */
    @PostMapping("/addComment")
    public String addComment(@RequestBody Map<String,Object> data, @RequestHeader("token") String token) {
        boolean b = commentService.addComment((Integer) data.get("photoId"), token, (String) data.get("content"));
        if (b) {
            return ResultFactory.createResult(CodeEnum.SUCCESS,"添加评论成功",null);
        } else {
            return ResultFactory.createResult(CodeEnum.FAIL,"添加评论失败",null);
        }
    }

    /**
     *  根据用户id获取评论
     * @param token
     * @return
     */
    @GetMapping("/getCommentByUserId")
    public String getCommentByUserId(@RequestHeader("token") String token, @RequestParam("pageNum") int pageNum) {
        return commentService.getCommentByUserId(token, pageNum);
    }

    /**
     *  删除评论
     * @param data
     * @return
     */
    @PostMapping("/deleteComment")
    public String deleteComment(@RequestBody Map<String,Object> data) {
        boolean b = commentService.deleteComment(String.valueOf(data.get("photoId") ));
        if (b) {
            return ResultFactory.createResult(CodeEnum.SUCCESS,"删除评论成功",null);
        } else {
            return ResultFactory.createResult(CodeEnum.FAIL,"删除评论失败",null);
        }
    }
}
